package com.dingding.service.activity.core.dao.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.PromotionDayStat;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.service.activity.core.dao.IPromotionDayStatDao;

/**
 * @Description:推广商统计dao
 * @author fengshuonan
 * @date 2016年4月26日 下午8:35:54
 */
@Component("promotionDayStatDao")
public class PromotionDayStatDaoImpl extends BaseDaoImpl<PromotionDayStat> implements IPromotionDayStatDao {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description: 增加推广商统计次数
	 * @param shopId 推广商id
	 * @author fengshuonan
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public synchronized void addPromotionNumByType(Integer shopId) {
	
		/**
		 * 	查询当天是否有该推广商的记录：
		 * 		如果没有，则增加一条记录
		 * 		如果有，则在该条记录加1此关注次数
		 */
		PromotionDayStat dayStat = null;
		
		try {
			dayStat = this.queryCriteriaUnResult(
					Restrictions.and(
							Restrictions.eq("shopId", shopId),
							Restrictions.eq("recordDate", DateFormatUtil.yyyy_MM_ddDate(new Date()))));
			
			if(dayStat == null){
				
				Date date = DateFormatUtil.yyyy_MM_ddDate(new Date());
				
				PromotionDayStat record = new PromotionDayStat(shopId,1,date);
				
				this.save(record);
			}else{
				dayStat.setAttendNum(dayStat.getAttendNum() + 1);
			}
			
		} catch (Exception e) {
			
			log.error("查询是否有当天推广商记录时出错！PromotionDayStatDaoImpl : addPromotionNumByType()",e);
			return ;
		}
		
	}
}
